/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfo.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.modules.sys.entity.User;
import com.lczyfz.campusinfo.modules.sys.service.SystemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.campusinfo.entity.CampusinfoPick;
import com.lczyfz.campusinfo.modules.campusinfo.service.CampusinfoPickService;

import java.util.List;

/**
 * 拾物找回Controller
 * @author zms
 * @version 2018-04-16
 */
@Controller
@RequestMapping(value = "${adminPath}/campusinfo/campusinfoPick")
public class CampusinfoPickController extends BaseController {

	@Autowired
	private CampusinfoPickService campusinfoPickService;
	@Autowired
	private SystemService systemService;
	
	@ModelAttribute
	public CampusinfoPick get(@RequestParam(required=false) String id) {
		CampusinfoPick entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = campusinfoPickService.get(id);
		}
		if (entity == null){
			entity = new CampusinfoPick();
		}
		return entity;
	}
	
	@RequiresPermissions("campusinfo:campusinfoPick:view")
	@RequestMapping(value = {"list", ""})
	public String list(CampusinfoPick campusinfoPick, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CampusinfoPick> page = campusinfoPickService.findPage(new Page<CampusinfoPick>(request, response), campusinfoPick);
		List<CampusinfoPick> campusinfoPicks = campusinfoPickService.findList(campusinfoPick);
		model.addAttribute("campusinfoPicks", campusinfoPicks);
		model.addAttribute("page", page);
		return "modules/campusinfo/campusinfoPickList";
	}

	@RequiresPermissions("campusinfo:campusinfoPick:view")
	@RequestMapping(value = "form")
	public String form(CampusinfoPick campusinfoPick, Model model) {
		User campusinfoUser = new User();
		List<User> campusinfoUseres = systemService.findUser(campusinfoUser);
		model.addAttribute("campusinfoUseres", campusinfoUseres);
		model.addAttribute("campusinfoPick", campusinfoPick);
		return "modules/campusinfo/campusinfoPickForm";
	}

	@RequiresPermissions("campusinfo:campusinfoPick:edit")
	@RequestMapping(value = "save")
	public String save(CampusinfoPick campusinfoPick, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, campusinfoPick)){
			return form(campusinfoPick, model);
		}
		campusinfoPickService.save(campusinfoPick);
		addMessage(redirectAttributes, "保存拾物找回成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfo/campusinfoPick/?repage";
	}
	
	@RequiresPermissions("campusinfo:campusinfoPick:edit")
	@RequestMapping(value = "delete")
	public String delete(CampusinfoPick campusinfoPick, RedirectAttributes redirectAttributes) {
		campusinfoPickService.delete(campusinfoPick);
		addMessage(redirectAttributes, "删除拾物找回成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfo/campusinfoPick/?repage";
	}

}